/*
 * Copyright (C) 2010-2011 sunjumper@163.com
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *  http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package mf.mfrpc.serialize;

import java.util.Map;

/**
 * 参数类型。
 */
public final class ParameterType {

    public static final byte NULL = 0;

    public static final byte STRING = 1;

    public static final byte DATASOURCE = 2;

    public static final byte DATASOURCES = 3;

    public static final byte NUMBER = 4;

    public static final byte BOOLEAN = 5;

    public static final byte ARRAY = 6;

    public static final byte MAP = 7;

    public static final byte ENUM = 8;

    public static final byte OBJECT = 9;

    public static byte getType(Object obj) {
        if (obj == null)
            return NULL;

        if (obj instanceof CharSequence)
            return STRING;

        if (obj instanceof DataSource)
            return DATASOURCE;

        if (obj instanceof DataSourceIterator)
            return DATASOURCES;

        if (obj instanceof Number)
            return NUMBER;

        if (obj instanceof Boolean)
            return BOOLEAN;

        if (obj instanceof Iterable || obj.getClass().isArray())
            return ARRAY;

        if (obj instanceof Map)
            return MAP;

        if (obj instanceof Enum)
            return ENUM;

        return OBJECT;
    }

    public static String getType(byte type) {
        switch (type) {
            case NULL:
                return "null";
            case STRING:
                return "string";
            case DATASOURCE:
                return "datasource";
            case DATASOURCES:
                return "datasources";
            case NUMBER:
                return "number";
            case BOOLEAN:
                return "boolean";
            case ARRAY:
                return "array";
            case MAP:
                return "map";
            case ENUM:
                return "enum";
            default:
                return "object";
        }
    }
}
